<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2019 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 老猫 <thinkcmf@126.com>
// +----------------------------------------------------------------------
namespace app\admin\model;

use api\index\controller\CommonController;
use think\Model;

class MemberModel extends Model
{
    const STATUS_NORMAL = 0;//正常
    const STATUS_BAN    = 1;//拉黑
    const NOT_VIP       = 0; //不是vip
    const IS_VIP        = 1; //是会员
    const STATUS        = ['正常', '已拉黑'];
    const VIP           = ['否', '是'];
    const SEX           = ['未知', '男', '女'];
    const DEFAULT_LEVEL = 0;//默认等级0级
    const ONE_LEVEL     = 1;//一级
    const THREE_LEVEL   = 3;//三级
    const DEFAULT_COIN  = 0;//默认球币
    const NEW_NO        = 0;//非新人
    const NEW_YES       = 1;//是新人
    protected $autoWriteTimestamp = true;//自动写入时间戳
    protected $pk = 'id';//主键，可以用get查询

    //收获地址
    public function addresses()
    {
        return $this->hasMany('ReceiveAddressModel', 'member_id');
    }

    //签到
    public function signIn()
    {
        return $this->hasMany('SignRecordModel', 'menber_id');
    }

    //球币变动
    public function changeCoin()
    {
        return $this->hasMany('ChangeCoinModel', 'member_id');
    }

    //购买聊币记录
    public function buyContent()
    {
        return $this->hasMany('BuyContentModel', 'member_id');
    }

    //充值vip和聊币记录
    public function payOrder()
    {
        return $this->hasMany('PayOrderModel', 'member_id');
    }

    //后台专家
    public function users()
    {
        return $this->belongsToMany('UserModel', 'followers', 'user_id', 'member_id');
    }

    public function batOrder()
    {
        return $this->hasMany('BatOrderModel', 'member_id');
    }

    public function address()
    {
        return $this->hasMany('ReceiveAddressModel', 'member_id');
    }

    //阅读文章观看视频记录
    public function readLook()
    {
        return $this->hasMany('ReadLookModel', 'member_id');
    }

    //我收藏点赞的视频
    public function video()
    {
        return $this->belongsToMany('VideoModel','favorite','relation_id','member_id');
    }


    public function favorite()
    {
        return $this->hasMany('FavoriteModel', 'member_id');
    }


    //是否完成阅读和观看视频 今日任务
    public function task($type)
    {
        //是否有观看阅读记录
        if (!$readLook = $this->readLook()->where(['type' => $type])->order('create_time desc')->find()) {
            return false;
        }
        //观看和阅读记录是否是今天
        if (date('Y-m-d', $readLook->create_time) == date('Y-m-d', time())) {
            return true;
        }
        return false;

    }

    //聊吧是否收藏和点赞
    public function talk_is_favorite($relation_id, $type)
    {
        return $this->favorite()->where(['type' => $type, 'relation_type' => FavoriteModel::TALK_TYPE, 'relation_id' => $relation_id])->find() ? true : false;

    }

    //首页是否收藏和点赞
    public function home_is_favorite($relation_id, $type)
    {
        return $this->favorite()->where(['type' => $type, 'relation_type' => FavoriteModel::INDEX_TYPE, 'relation_id' => $relation_id])->find() ? true : false;
    }

}